﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Model;

namespace Maticsoft.Common
{
    public class XMLLib
    {
        public static void AddXML(string titleID, string titleName, string titleUrl, string titleTypeIds, string path)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(path);
            XmlElement xmlelem;
            XmlNodeList xmlnodelist = xmldoc.ChildNodes;
            foreach (XmlNode xmlenode in xmlnodelist)
            {
                if (xmlenode.Name.ToString() == "root")
                {
                    xmlelem = (XmlElement)xmlenode;

                    XmlElement xmlelemtitle = xmldoc.CreateElement("title");//创建信息节点

                    XmlAttribute xmlattrID = xmldoc.CreateAttribute("id");
                    xmlattrID.Value = titleID;
                    xmlelemtitle.Attributes.Append(xmlattrID);

                    XmlAttribute xmlattrName = xmldoc.CreateAttribute("name");
                    xmlattrName.Value = titleName;
                    xmlelemtitle.Attributes.Append(xmlattrName);

                    XmlAttribute xmlattrUrl = xmldoc.CreateAttribute("url");
                    xmlattrUrl.Value = titleUrl;
                    xmlelemtitle.Attributes.Append(xmlattrUrl);

                    XmlAttribute xmlattrTypeIds = xmldoc.CreateAttribute("typeids");
                    xmlattrTypeIds.Value = titleTypeIds;
                    xmlelemtitle.Attributes.Append(xmlattrTypeIds);

                    xmlelem.AppendChild(xmlelemtitle);
                    xmldoc.AppendChild(xmlelem);
                    xmldoc.Save(path);
                }
            }
        }

        public static void UpdateTitle(string id, string path, string strname, string strurl, string ids)
        {
            if (id != "")
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);

                XmlNode xn = xmldoc.SelectSingleNode(string.Format("root/title[@id={0}]", id));

                if (xn != null)
                {
                    //xn.Attributes[3].InnerText = ids;
                    xn.Attributes["typeids"].InnerText = ids;
                    xn.Attributes["url"].InnerText = strurl;
                    xn.Attributes["name"].InnerText = strname;
                }
                xmldoc.Save(path);
            }
        }

        public static void DelteTitle(string id, string path)
        {
            if (id != "")
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);
                XmlNode xn1 = xmldoc.SelectSingleNode("root");
                XmlNode xn = xmldoc.SelectSingleNode(string.Format("root/title[@id={0}]", id));

                xn1.RemoveChild(xn);
                xmldoc.Save(path);
            }
        }




        /// <summary>
        /// 获取集合
        /// </summary>
        /// <returns></returns>
        public static List<Title> getTitle(string strpath)
        {
            List<Title> listTitle = new List<Title>();
            IEnumerable<XElement> elLesson = getLessonXML(strpath);

            foreach (var les in elLesson)
            {
                Title title = new Title();

                title.TitleID = les.Attribute("id").Value;
                title.TitleName = les.Attribute("name").Value;
                title.TitleUrl = les.Attribute("url").Value;
                title.TitleTypeIds = les.Attribute("typeids").Value;

                listTitle.Add(title);
            }
            return listTitle;
        }

        public static IEnumerable<XElement> getLessonXML(string strpath)
        {
            //XElement element = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/Admin/Title/Title.xml");
            XElement element = XElement.Load(strpath);
            IEnumerable<XElement> elPart = element.Elements("title");
            return elPart;
        }


        /// <summary>
        /// 返回指定路径下XMl文件指定元素的指定属性值  
        /// </summary>
        /// <param name="strpath">指定路径</param>
        /// <param name="xmlElement">指定元素</param>
        /// <param name="xmlAttribute">指定属性</param>
        /// <param name="xmlwhereAttribute">指定条件属性</param>
        /// <param name="xmlwhereAttributeValue">指定条件属性值</param>
        /// <returns></returns>
        public static string getXmlValue(string strpath, string xmlElement, string xmlwhereAttribute, string xmlwhereAttributeValue, string xmlAttribute)
        {
            XDocument xmlDoc = XDocument.Load(strpath);
            var results = from c in xmlDoc.Descendants(xmlElement)
                          select c;
            string s = "";
            string virsion = "";
            foreach (var result in results)
            {
                s = result.Attribute(xmlwhereAttribute).Value.ToString();
                if (s == xmlwhereAttributeValue)
                {
                    virsion = result.Attribute(xmlAttribute).Value.ToString();
                    break;
                }
            }
            return virsion;
        }

    }
}
